﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace WebNetCorePhoneErp.Handler
{
    /// <summary>
    /// 全局异常过滤器
    /// </summary>
    /// <remarks>
    /// 创建时间：2021-6-10 17:19:48
    /// 作者：王浩力
    /// </remarks>
    public class CustomExceptionFilter : IExceptionFilter
    {

        public void OnException(ExceptionContext context)
        {
            //LogHelpter.AddLog(context.Exception.ToString());
            var ex = context.Exception.InnerException ?? context.Exception;
            var ex2 = ex.InnerException ?? ex;
            var ex3 = ex2.InnerException ?? ex2;
            string msg = ex3.Message;

            ////记录异常日志 2021-4-16 17:42:14 添加
            //ISysExceptionLogBLL sysExceptionLogBLL = (ISysExceptionLogBLL)context.HttpContext.RequestServices.GetService(typeof(ISysExceptionLogBLL));
            //if (sysExceptionLogBLL != null)
            //{ 
            //    sysExceptionLogBLL.AddExceptionLog(ex3);
            //}

            Console.WriteLine("捕获到异常了：" + msg);
            //System.Diagnostics.Debug.WriteLine("捕获到异常了：" + msg);
            var result = new { Code = 550, Msg = msg };
            context.Result = new JsonResult(result) { ContentType = "application/json; charset=utf-8" };
        }
    }
}
